"""
Created on 2022-05-26
@author:刘飞
@description:创建数据表
"""
import datetime
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.orm import relationship
from databases import Base


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True, doc='用户id')
    username = Column(String(150), unique=True, index=True, doc='用户名')
    email = Column(String(255), unique=True, nullable=True, doc='邮箱')
    hashed_password = Column(String(255), doc='密码')
    is_active = Column(Boolean, default=True, doc='是否激活')
    user_detail = relationship("UserDetail", back_populates="user")  # 方便查询，不生成字段

    created_time = Column(DateTime, default=datetime.datetime.now, doc='创建时间')
    updated_time = Column(DateTime, default=datetime.datetime.now(), doc='更新时间')


class UserDetail(Base):
    __tablename__ = "user_detail"

    id = Column(Integer, primary_key=True, index=True)
    full_name = Column(String(255), index=True, nullable=True, doc='真实姓名')
    nick_name = Column(String(255), index=True, nullable=True, doc='昵称')
    address = Column(String(255), nullable=True, doc='家庭地址')
    user_id = Column(Integer, ForeignKey("users.id"), doc='用户id')
    user = relationship("User", back_populates="user_detail")  # 方便查询，不生成字段

    created_time = Column(DateTime, default=datetime.datetime.now, doc='创建时间')
    updated_time = Column(DateTime, default=datetime.datetime.now(), doc='更新时间')
